<?php

namespace app\yperp\common;

use PhpOffice\PhpWord\TemplateProcessor;
use think\Exception;

class PhpWordTemplateProcessor
{
    /**
     * 首页
     * @author 蔡伟明 <314013107@qq.com>
     * @return mixed
     */
    static public function todo($data = [], $save_path = 'text.docx')
    {
        //1.创建模板对象
        $templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor($save_path);

        //2.插入数据
        //插入文字
        foreach ($data['content'] as $targ =>  $value) {

            $templateProcessor->setValue($targ, $value);
        }

        if (isset($data['images']) && !empty($data['images'])) {
            $imaes = $data['images'];
            $templateProcessor->cloneRow('images', count($imaes));

            foreach ($imaes as $i => $item) {

                $templateProcessor->setImageValue(sprintf('images#%d', $i + 1), $item);
            }
        } else {
            $templateProcessor->setValue('images', '');
        }


        if (isset($data['files']) && !empty($data['files'])) {
            $files = $data['files'];
            $templateProcessor->cloneRow('files', count($files));

            foreach ($files as $i => $link) {

                $templateProcessor->setValue(sprintf('files#%d', $i + 1), $link);
            }
        } else {
            $templateProcessor->setValue('files', '');
        }




        if (isset($data['reply']) && !empty($data['reply'])) {
            $imaes = $data['reply'];
            $templateProcessor->cloneRow('reply_type', count($imaes));

            foreach ($imaes as $i => $item) {

                $templateProcessor->setValue(sprintf('reply_type#%d', $i + 1), $item['reply_type']);
                $templateProcessor->setValue(sprintf('reply_content#%d', $i + 1), $item['reply_content']);
                $templateProcessor->setValue(sprintf('reply_time#%d', $i + 1), $item['reply_time']);
            }
        } else {
            $templateProcessor->setValue('reply', '');
        }



        //检测目录，没有则创建
        $file_path   = env('root_path') . 'runtime/file.docx';
        try {
            //图片保存到服务器
            // $templateProcessor->saveAs($file_path);
            // $templateProcessor->save();






            $filename = iconv("utf-8", "gb2312", "测试");   //防止有的浏览器下载之后中文乱码

            header("Content-type: application/vnd.ms-word");
            header("Content-Disposition:attachment;filename=" . $filename . ".docx");
            header('Cache-Control: max-age=0');
            $templateProcessor->saveAs("php://output");
        } catch (Exception $e) {
            echo '保存出错，请联系管理员';
        }
    }

    protected function cloneRowValues(&$templateProcessor, $datas)
    {
        // $templateProcessor->cloneRow('images', count($imaes));
        $i = 1;
        foreach ($datas as $targ => $item) {
            if ($i == 1) {
                $templateProcessor->cloneRow($targ, count($datas));
            }
            $templateProcessor->setValue(sprintf($targ . '#%d', $i), $item);
            $i++;
        }
    }
    protected function cloneRowImage(&$templateProcessor, $imaes)
    {

        $i = 1;
        foreach ($imaes as $targ => $item) {
            if ($i == 1) {
                $templateProcessor->cloneRow('images', count($imaes));
            }
            $templateProcessor->setImageValue(sprintf($targ . '#%d', $i), $item);
            $i++;
        }
    }
}
